package com.alibaba.citrus.cr.unified.inventory.repository.impl;

import com.alibaba.citrus.cr.unified.inventory.model.AllocateOrderEntity;
import com.alibaba.citrus.cr.unified.inventory.repository.AllocateOrderDomainRepository;
import com.epoch.app.crunifiedinventory.domain.allocateorder.dto.UpdateStatusRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorder.service.AllocateOrderService;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年01月24日 11:48
 **/
@Component
public class AllocateOrderDomainRepositoryImpl implements AllocateOrderDomainRepository {

    @Resource
    private AllocateOrderService allocateOrderService;
    @Resource
    private AllocateOrderDetailService allocateOrderDetailService;

    @Override
    public void updateStatus(AllocateOrderEntity allocateOrderEntity) {

        //TODO 考虑是否需要报错
        if (allocateOrderEntity == null || allocateOrderEntity.getAllocateOrder() == null ||
                CollectionUtils.isEmpty(allocateOrderEntity.getDetailList())){
            return;
        }
        allocateOrderEntity.getDetailList().forEach(detail -> {
            com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.UpdateStatusRequest detailUpdateStatusRequest =
                    com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.UpdateStatusRequest.builder()
                            .id(detail.getId()).status(detail.getStatus()).build();
            allocateOrderDetailService.updateStatus(detailUpdateStatusRequest);
        });
        UpdateStatusRequest mainUpdateRequest = UpdateStatusRequest.builder()
                .id(allocateOrderEntity.getAllocateOrder().getId())
                .status(allocateOrderEntity.getAllocateOrder().getStatus()).build();
        allocateOrderService.updateStatus(mainUpdateRequest);
    }
}
